home *** CD-ROM | disk | FTP | other *** search
/ Hot Metal Pro 4.0 / Hot Metal Pro 4.0.iso / HMPRO4 / cgi.z / wwwboard.pl < prev   
Encoding:
Perl Script  |  1996-04-17  |  17.1 KB  |  540 lines

  1. #!/usr/local/bin/perl
  2.  
  3. ###########################################################################
  4. # WWWBoard
  5. # Version 2.0 ALPHA 2
  6. # Created by Matt Wright
  7. # Created on:  10/21/95         Last Modified on:  11/25/95
  8. # I can be reached at:          mattw@misha.net
  9. # Scripts Found at:             http://www.worldwidemart.com/scripts/
  10. ###########################################################################
  11.  
  12. ###########################################################################
  13. # Define Variables
  14.  
  15. $basedir = "/path/to/wwwboard";
  16. $baseurl = "http://your.host.xxx/wwwboard";
  17. $cgi_url = "http://your.host.xx/cgi-bin/wwwboard.pl";
  18.  
  19. $mesgdir = "messages";
  20. $datafile = "data.txt";
  21. $mesgfile = "wwwboard.html";
  22. $faqfile = "faq.html";
  23.  
  24. $ext = "html";
  25.  
  26. $title = "WWWBoard Version 2.0 Test";
  27.  
  28. # Done
  29. ###########################################################################
  30.  
  31. ###########################################################################
  32. # Configure Options
  33.  
  34. $show_faq = 1;        # 1 - YES; 0 = NO
  35. $allow_html = 1;    # 1 = YES; 0 = NO
  36. $quote_text = 1;    # 1 = YES; 0 = NO
  37. $subject_line = 0;    # 0 = Quote Subject Editable; 1 = Quote Subject 
  38.             #   UnEditable; 2 = Don't Quote Subject, Editable.
  39. $use_time = 1;        # 1 = YES; 0 = NO
  40.  
  41. # Done
  42. ###########################################################################
  43.  
  44. # Get the Data Number
  45. &get_number;
  46.  
  47. # Get Form Information
  48. &parse_form;
  49.  
  50. # Put items into nice variables
  51. &get_variables;
  52.  
  53. # Open the new file and write information to it.
  54. &new_file;
  55.  
  56. # Open the Main WWWBoard File to add link
  57. &main_page;
  58.  
  59. # Now Add Thread to Individual Pages
  60. if ($num_followups >= 1) {
  61.    &thread_pages;
  62. }
  63.  
  64. # Return the user HTML
  65. &return_html;
  66.  
  67. # Increment Number
  68. &increment_num;
  69.  
  70. ############################
  71. # Get Data Number Subroutine
  72.  
  73. sub get_number {
  74.    open(NUMBER,"$basedir/$datafile");
  75.    $num = <NUMBER>;
  76.    close(NUMBER);
  77.    if ($num == 99999)  {
  78.       $num = "1";
  79.    }
  80.    else {
  81.       $num++;
  82.    }
  83. }
  84.  
  85. #######################
  86. # Parse Form Subroutine
  87.  
  88. sub parse_form {
  89.  
  90.    # Get the input
  91.    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  92.  
  93.    # Split the name-value pairs
  94.    @pairs = split(/&/, $buffer);
  95.  
  96.    foreach $pair (@pairs) {
  97.       ($name, $value) = split(/=/, $pair);
  98.  
  99.       # Un-Webify plus signs and %-encoding
  100.       $value =~ tr/+/ /;
  101.       $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  102.       $value =~ s/<!--(.|\n)*-->//g;
  103.  
  104.       if ($allow_html != 1) {
  105.          $value =~ s/<([^>]|\n)*>//g;
  106.       }
  107.       else {
  108.          unless ($name eq 'body') {
  109.         $value =~ s/<([^>]|\n)*>//g;
  110.          }
  111.       }
  112.  
  113.       $FORM{$name} = $value;
  114.    }
  115.  
  116. }
  117.  
  118. ###############
  119. # Get Variables
  120.  
  121. sub get_variables {
  122.  
  123.    if ($FORM{'followup'}) {
  124.       $followup = "1";
  125.       @followup_num = split(/,/,$FORM{'followup'});
  126.       $num_followups = @followups = @followup_num;
  127.       $last_message = pop(@followups);
  128.       $origdate = "$FORM{'origdate'}";
  129.       $origname = "$FORM{'origname'}";
  130.       $origsubject = "$FORM{'origsubject'}";
  131.    }
  132.    else {
  133.       $followup = "0";
  134.    }
  135.  
  136.    if ($FORM{'name'}) {
  137.       $name = "$FORM{'name'}";
  138.       $name =~ s/"//g;
  139.       $name =~ s/<//g;
  140.       $name =~ s/>//g;
  141.       $name =~ s/\&//g;
  142.    }
  143.    else {
  144.       &error(no_name);
  145.    }
  146.  
  147.    if ($FORM{'email'} =~ /.*\@.*\..*/) {
  148.       $email = "$FORM{'email'}";
  149.    }
  150.  
  151.    if ($FORM{'subject'}) {
  152.       $subject = "$FORM{'subject'}";
  153.       $subject =~ s/\&/\&\;/g;
  154.       $subject =~ s/"/\"\;/g;
  155.    }
  156.    else {
  157.       &error(no_subject);
  158.    }
  159.  
  160.    if ($FORM{'url'} =~ /.*\:.*\..*/ && $FORM{'url_title'}) {
  161.       $message_url = "$FORM{'url'}";
  162.       $message_url_title = "$FORM{'url_title'}";
  163.    }
  164.  
  165.    if ($FORM{'img'} =~ /.*tp:\/\/.*\..*/) {
  166.       $message_img = "$FORM{'img'}";
  167.    }
  168.  
  169.    if ($FORM{'body'}) {
  170.       $body = "$FORM{'body'}";
  171.       $body =~ s/\cM//g;
  172.       $body =~ s/\n\n/<p>/g;
  173.       $body =~ s/\n/<br>/g;
  174.  
  175.       $body =~ s/</</g; 
  176.       $body =~ s/>/>/g; 
  177.       $body =~ s/"/"/g;
  178.    }
  179.    else {
  180.       &error(no_body);
  181.    }
  182.  
  183.    if ($quote_text == 1) {
  184.       $hidden_body = "$body";
  185.       $hidden_body =~ s/</</g;
  186.       $hidden_body =~ s/>/>/g;
  187.       $hidden_body =~ s/"/"/g;
  188.    }
  189.  
  190.    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  191.  
  192.    if ($sec < 10) {
  193.       $sec = "0$sec";
  194.    }
  195.    if ($min < 10) {
  196.       $min = "0$min";
  197.    }
  198.    if ($hour < 10) {
  199.       $hour = "0$hour";
  200.    }
  201.    if ($mon < 10) {
  202.       $mon = "0$mon";
  203.    }
  204.    if ($mday < 10) {
  205.       $mday = "0$mday";
  206.    }
  207.  
  208.    $month = ($mon + 1);
  209.  
  210.    @months = ("January","February","March","April","May","June","July","August","September","October","November","December");
  211.  
  212.    if ($use_time == 1) {
  213.       $date = "$hour\:$min\:$sec $month/$mday/$year";
  214.    }
  215.    else {
  216.       $date = "$month/$mday/$year";
  217.    }
  218.    chop($date) if ($date =~ /\n$/);
  219.  
  220.    $long_date = "$months[$mon] $mday, 19$year at $hour\:$min\:$sec";
  221. }      
  222.  
  223. #####################
  224. # New File Subroutine
  225.  
  226. sub new_file {
  227.  
  228.    open(NEWFILE,">$basedir/$mesgdir/$num\.$ext") || die $!;
  229.    print NEWFILE "<html>\n";
  230.    print NEWFILE "  <head>\n";
  231.    print NEWFILE "    <title>$subject</title>\n";
  232.    print NEWFILE "  </head>\n";
  233.    print NEWFILE "  <body>\n";
  234.    print NEWFILE "    <center>\n";
  235.    print NEWFILE "      <h1>$subject</h1>\n";
  236.    print NEWFILE "    </center>\n";
  237.    print NEWFILE "<hr size=7 width=75%>\n";
  238.    if ($show_faq == 1) {
  239.       print NEWFILE "<center>[ <a href=\"#followups\">Follow Ups</a> ] [ <a href=\"#postfp\">Post Followup</a> ] [ <a href=\"$baseurl/$mesgfile\">$title</a> ] [ <a href=\"$baseurl/$faqfile\">FAQ</a> ]</center>\n";
  240.    }
  241.    else {
  242.       print NEWFILE "<center>[ <a href=\"#followups\">Follow Ups</a> ] [ <a href=\"#postfp\">Post Followup</a> ] [ <a href=\"$baseurl/$mesgfile\">$title</a> ]</center>\n";
  243.    }
  244.    print NEWFILE "<hr size=7 width=75%><p>\n";
  245.  
  246.    print NEWFILE "Posted by ";
  247.  
  248.    if ($email) {
  249.       print NEWFILE "<a href=\"mailto:$email\">$name</a> on $long_date:<p>\n";
  250.    }
  251.    else {
  252.       print NEWFILE "$name on $long_date:<p>\n";
  253.    }
  254.  
  255.    if ($followup == 1) {
  256.       print NEWFILE "In Reply to: <a href=\"$last_message\.$ext\">$origsubject</a> posted by ";
  257.  
  258.       if ($origemail) {
  259.          print NEWFILE "<a href=\"$origemail\">$origname</a> on $origdate:<p>\n";
  260.       }
  261.       else {
  262.          print NEWFILE "$origname on $origdate:<p>\n";
  263.       }
  264.    }
  265.  
  266.    if ($message_img) {
  267.       print NEWFILE "<center><img src=\"$message_img\"></center><p>\n";
  268.    }
  269.    print NEWFILE "$body\n";
  270.    print NEWFILE "<br>\n";
  271.    if ($message_url) {
  272.       print NEWFILE "<ul><li><a href=\"$message_url\">$message_url_title</a></ul>\n";
  273.    }
  274.    print NEWFILE "<br><hr size=7 width=75%><p>\n";
  275.    print NEWFILE "<a name=\"followups\">Follow Ups:</a><br>\n";
  276.    print NEWFILE "<ul><!--insert: $num-->\n";
  277.    print NEWFILE "</ul><!--end: $num-->\n";
  278.    print NEWFILE "<br><hr size=7 width=75%><p>\n";
  279.    print NEWFILE "<a name=\"postfp\">Post a Followup</a><p>\n";
  280.    print NEWFILE "<form method=POST action=\"$cgi_url\">\n";
  281.    print NEWFILE "<input type=hidden name=\"followup\" value=\"";
  282.    if ($followup == 1) {
  283.       foreach $followup_num (@followup_num) {
  284.          print NEWFILE "$followup_num,";
  285.       }
  286.    }
  287.    print NEWFILE "$num\">\n";
  288.    print NEWFILE "<input type=hidden name=\"origname\" value=\"$name\">\n";
  289.    if ($email) {
  290.       print NEWFILE "<input type=hidden name=\"origemail\" value=\"$email\">\n";
  291.    }
  292.    print NEWFILE "<input type=hidden name=\"origsubject\" value=\"$subject\">\n";
  293.    print NEWFILE "<input type=hidden name=\"origdate\" value=\"$long_date\">\n";
  294.    print NEWFILE "Name: <input type=text name=\"name\" size=50><br>\n";
  295.    print NEWFILE "E-Mail: <input type=text name=\"email\" size=50><p>\n";
  296.    if ($subject_line == 1) {
  297.       if ($subject_line =~ /^Re:/) {
  298.          print NEWFILE "<input type=hidden name=\"subject\" value=\"$subject\">\n";
  299.          print NEWFILE "Subject: <b>$subject</b><p>\n";
  300.       }
  301.       else {
  302.          print NEWFILE "<input type=hidden name=\"subject\" value=\"Re: $subject\">\n";
  303.          print NEWFILE "Subject: <b>Re: $subject</b><p>\n";
  304.       }
  305.    } 
  306.    elsif ($subject_line == 2) {
  307.       print NEWFILE "Subject: <input type=text name=\"subject\" size=50><p>\n";
  308.    }
  309.    else {
  310.       if ($subject =~ /^Re:/) {
  311.          print NEWFILE "Subject: <input type=text name=\"subject\"value=\"$subject\" size=50><p>\n";
  312.       }
  313.       else {
  314.          print NEWFILE "Subject: <input type=text name=\"subject\" value=\"Re: $subject\" size=50><p>\n";
  315.       }
  316.    }
  317.    print NEWFILE "Comments:<br>\n";
  318.    print NEWFILE "<textarea name=\"body\" COLS=50 ROWS=10>\n";
  319.    if ($quote_text == 1) {
  320.       @chunks_of_body = split(/\<\;p\>\;/,$hidden_body);
  321.       foreach $chunk_of_body (@chunks_of_body) {
  322.          @lines_of_body = split(/\<\;br\>\;/,$chunk_of_body);
  323.          foreach $line_of_body (@lines_of_body) {
  324.             print NEWFILE ": $line_of_body\n";
  325.          }
  326.          print NEWFILE "\n";
  327.       }
  328.    }
  329.    print NEWFILE "</textarea>\n";
  330.    print NEWFILE "<p>\n";
  331.    print NEWFILE "Optional Link URL: <input type=text name=\"url\" size=50><br>\n";
  332.    print NEWFILE "Link Title: <input type=text name=\"url_title\" size=48><br>\n";
  333.    print NEWFILE "Optional Image URL: <input type=text name=\"img\" size=49><p>\n";
  334.    print NEWFILE "<input type=submit value=\"Submit Follow Up\"> <input type=reset>\n";
  335.    print NEWFILE "<p><hr size=7 width=75%>\n";
  336.    if ($show_faq == 1) {
  337.       print NEWFILE "<center>[ <a href=\"#followups\">Follow Ups</a> ] [ <a href=\"#postfp\">Post Followup</a> ] [ <a href=\"$baseurl/$mesgfile\">$title</a> ] [ <a href=\"$baseurl/$faqfile\">FAQ</a> ]</center>\n";
  338.    }
  339.    else {
  340.       print NEWFILE "<center>[ <a href=\"#followups\">Follow Ups</a> ] [ <a href=\"#postfp\">Post Followup</a> ] [ <a href=\"$baseurl/$mesgfile\">$title</a> ]</center>\n";
  341.    }
  342.    print NEWFILE "</body></html>\n";
  343.    close(NEWFILE);
  344. }
  345.  
  346. ###############################
  347. # Main WWWBoard Page Subroutine
  348.  
  349. sub main_page {
  350.    open(MAIN,"$basedir/$mesgfile") || die $!;
  351.    @main = <MAIN>;
  352.    close(MAIN);
  353.  
  354.    open(MAIN,">$basedir/$mesgfile") || die $!;
  355.    if ($followup == 0) {
  356.       foreach $main_line (@main) {
  357.          if ($main_line =~ /<!--begin-->/) {
  358.             print MAIN "<!--begin-->\n";
  359.         print MAIN "<!--top: $num--><li><a href=\"$mesgdir/$num\.$ext\">$subject</a> - <b>$name</b> <i>$date</i>\n";
  360.             print MAIN "(<!--responses: $num-->0)\n";
  361.             print MAIN "<ul><!--insert: $num-->\n";
  362.             print MAIN "</ul><!--end: $num-->\n";
  363.          }
  364.          else {
  365.             print MAIN "$main_line";
  366.          }
  367.       }
  368.    }
  369.    else {
  370.       foreach $main_line (@main) {
  371.      $work = 0;
  372.          if ($main_line =~ /<ul><!--insert: $last_message-->/) {
  373.             print MAIN "<ul><!--insert: $last_message-->\n";
  374.             print MAIN "<!--top: $num--><li><a href=\"$mesgdir/$num\.$ext\">$subject</a> - <b>$name</b> <i>$date</i>\n";
  375.             print MAIN "(<!--responses: $num-->0)\n";
  376.             print MAIN "<ul><!--insert: $num-->\n";
  377.             print MAIN "</ul><!--end: $num-->\n";
  378.          }
  379.          elsif ($main_line =~ /\(<!--responses: (.*)-->(.*)\)/) {
  380.             $response_num = $1;
  381.             $num_responses = $2;
  382.             $num_responses++;
  383.             foreach $followup_num (@followup_num) {
  384.                if ($followup_num == $response_num) {
  385.                   print MAIN "(<!--responses: $followup_num-->$num_responses)\n";
  386.           $work = 1;
  387.                }
  388.             }
  389.             if ($work != 1) {
  390.                print MAIN "$main_line";
  391.             }
  392.          }
  393.          else {
  394.             print MAIN "$main_line";
  395.          }
  396.       }
  397.    }
  398.    close(MAIN);
  399. }
  400.  
  401. ############################################
  402. # Add Followup Threading to Individual Pages
  403. sub thread_pages {
  404.  
  405.    foreach $followup_num (@followup_num) {
  406.       open(FOLLOWUP,"$basedir/$mesgdir/$followup_num\.$ext");
  407.       @followup_lines = <FOLLOWUP>;
  408.       close(FOLLOWUP);
  409.  
  410.       open(FOLLOWUP,">$basedir/$mesgdir/$followup_num\.$ext");
  411.       foreach $followup_line (@followup_lines) {
  412.          $work = 0;
  413.          if ($followup_line =~ /<ul><!--insert: $last_message-->/) {
  414.         print FOLLOWUP "<ul><!--insert: $last_message-->\n";
  415.             print FOLLOWUP "<!--top: $num--><li><a href=\"$num\.$ext\">$subject</a> <b>$name</b> <i>$date</i>\n";
  416.             print FOLLOWUP "(<!--responses: $num-->0)\n";
  417.             print FOLLOWUP "<ul><!--insert: $num-->\n";
  418.             print FOLLOWUP "</ul><!--end: $num-->\n";
  419.          }
  420.          elsif ($followup_line =~ /\(<!--responses: (.*)-->(.*)\)/) {
  421.             $response_num = $1;
  422.             $num_responses = $2;
  423.             $num_responses++;
  424.             foreach $followup_num (@followup_num) {
  425.                if ($followup_num == $response_num) {
  426.                   print FOLLOWUP "(<!--responses: $followup_num-->$num_responses)\n";
  427.                   $work = 1;
  428.                }
  429.             }
  430.             if ($work != 1) {
  431.                print FOLLOWUP "$followup_line";
  432.             }
  433.          }
  434.          else {
  435.             print FOLLOWUP "$followup_line";
  436.          }
  437.       }
  438.       close(FOLLOWUP);
  439.    }
  440. }
  441.  
  442. sub return_html {
  443.    print "Content-type: text/html\n\n";
  444.    print "<html><head><title>Message Added: $subject</title></head>\n";
  445.    print "<body><center><h1>Message Added: $subject</h1></center>\n";
  446.    print "The following information was added to the message board:<p><hr size=7 width=75%><p>\n";
  447.    print "<b>Name:</b> $name<br>\n";
  448.    print "<b>E-Mail:</b> $email<br>\n";
  449.    print "<b>Subject:</b> $subject<br>\n";
  450.    print "<b>Body of Message:</b><p>\n";
  451.    print "$body<p>\n";
  452.    if ($message_url) {
  453.       print "<b>Link:</b> <a href=\"$message_url\">$message_url_title</a><br>\n";
  454.    }
  455.    if ($message_img) {
  456.       print "<b>Image:</b> <img src=\"$message_img\"><br>\n";
  457.    }
  458.    print "<b>Added on Date:</b> $date<p>\n";
  459.    print "<hr size=7 width=75%>\n";
  460.    print "<center>[ <a href=\"$baseurl/$mesgdir/$num\.$ext\">Go to Your Message</a> ] [ <a href=\"$baseurl/$mesgfile\">$title</a> ]</center>\n";
  461.    print "</body></html>\n";
  462. }
  463.  
  464. sub increment_num {
  465.    open(NUM,">$basedir/$datafile") || die $!;
  466.    print NUM "$num";
  467.    close(NUM);
  468. }
  469.  
  470. sub error {
  471.    $error = $_[0];
  472.  
  473.    print "Content-type: text/html\n\n";
  474.  
  475.    if ($error eq 'no_name') {
  476.       print "<html><head><title>$title ERROR: No Name</title></head>\n";
  477.       print "<body><center><h1>ERROR: No Name</h1></center>\n";
  478.       print "You forgot to fill in the 'Name' field in your posting.  Correct it below and re-submit.  The necessary fields are: Name, Subject and Message.<p><hr size=7 width=75%><p>\n";
  479.       &rest_of_form;
  480.    }
  481.    elsif ($error eq 'no_subject') {
  482.       print "<html><head><title>$title ERROR: No Subject</title></head>\n";
  483.       print "<body><center><h1>ERROR: No Subject</h1></center>\n";
  484.       print "You forgot to fill in the 'Subject' field in your posting.  Correct it below and re-submit.  The necessary fields are: Name, Subject and Message.<p><hr size=7 width=75%><p>\n";
  485.       &rest_of_form;
  486.    }
  487.    elsif ($error eq 'no_body') {
  488.       print "<html><head><title>$title ERROR: No Message</title></head>\n";
  489.       print "<body><center><h1>ERROR: No Message</h1></center>\n";
  490.       print "You forgot to fill in the 'Message' fieldin your posting.  Correct it below and re-submit.  The necessary fields are: Name, Subjectand Message.<p><hr size=7 width=75%><p>\n";
  491.       &rest_of_form;
  492.    }
  493.    else {
  494.       print "ERROR!  Undefined.\n";
  495.    }
  496.    exit;
  497. }
  498.  
  499. sub rest_of_form {
  500.  
  501.    print "<form method=POST action=\"$cgi_url\">\n";
  502.  
  503.    if ($followup == 1) {
  504.       print "<input type=hidden name=\"origsubject\" value=\"$FORM{'origsubject'}\">\n";
  505.       print "<input type=hidden name=\"origname\" value=\"$FORM{'origname'}\">\n";
  506.       print "<input type=hidden name=\"origemail\" value=\"$FORM{'origemail'}\">\n";
  507.       print "<input type=hidden name=\"origdate\" value=\"$FORM{'origdate'}\">\n";
  508.       print "<input type=hidden name=\"followup\" value=\"$FORM{'followup'}\">\n";
  509.    }
  510.    print "Name: <input type=text name=\"name\" value=\"$FORM{'name'}\" size=50><br>\n";
  511.    print "E-Mail: <input type=text name=\"email\" value=\"$FORM{'email'}\" size=50><p>\n";
  512.    if ($subject_line == 1) {
  513.       print "<input type=hidden name=\"subject\" value=\"$FORM{'subject'}\">\n";
  514.       print "Subject: <b>$FORM{'subject'}</b><p>\n";
  515.    } 
  516.    else {
  517.       print "Subject: <input type=text name=\"subject\" value=\"$FORM{'subject'}\" size=50><p>\n";
  518.    }
  519.    print "Message:<br>\n";
  520.    print "<textarea COLS=50 ROWS=10 name=\"body\">\n";
  521.    $FORM{'body'} =~ s/</</g;
  522.    $FORM{'body'} =~ s/>/>/g;
  523.    $FORM{'body'} =~ s/"/"/g;
  524.    print "$FORM{'body'}\n";
  525.    print "</textarea><p>\n";
  526.    print "Optional Link URL: <input type=text name=\"url\" value=\"$FORM{'url'}\" size=45><br>\n";
  527.    print "Link Title: <input type=text name=\"url_title\" value=\"$FORM{'url_title'}\" size=50><br>\n";
  528.    print "Optional Image URL: <input type=text name=\"img\" value=\"$FORM{'img'}\" size=45><p>\n";
  529.    print "<input type=submit value=\"Post Message\"> <input type=reset>\n";
  530.    print "</form>\n";
  531.    print "<br><hr size=7 width=75%>\n";
  532.    if ($show_faq == 1) {
  533.       print "<center>[ <a href=\"#followups\">Follow Ups</a> ] [ <a href=\"#postfp\">Post Followup</a> ] [ <a href=\"$baseurl/$mesgfile\">$title</a> ] [ <a href=\"$baseurl/$faqfile\">FAQ</a> ]</center>\n";
  534.    }
  535.    else {
  536.       print "<center>[ <a href=\"#followups\">Follow Ups</a> ] [ <a href=\"#postfp\">Post Followup</a> ] [ <a href=\"$baseurl/$mesgfile\">$title</a> ]</center>\n";
  537.    }
  538.    print "</body></html>\n";
  539. }
  540.